package com.example.project.controller;

import com.example.project.dto.ExterUnitCatalogDTO;
import com.example.project.dto.ExterUnitCatalogDTO2;
import com.example.project.dto.ExterUnitCatalogPageDTO;
import com.example.project.entity.ExterUnitCatalog;
import com.example.project.service.ExterUnitCatalogService;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * (ExterUnitCatalog)表控制层
 *
 * @author makejava
 * @since 2024-02-23 15:18:22
 */
@RestController
@RequestMapping("exterUnitCatalog")
public class ExterUnitCatalogController {
    /**
     * 服务对象
     */
    @Resource
    private ExterUnitCatalogService exterUnitCatalogService;

    /**
     * 分页查询
     *
     * param exterUnitCatalog 筛选条件
     * param pageRequest      分页对象
     * @return 查询结果
     */
//    @CrossOrigin
//    @PostMapping("findPage")
//    public ResponseEntity<Page<ExterUnitCatalog>> queryByPage(@RequestBody ExterUnitCatalogPageDTO data) {
//        com.example.project.entity.Page page = data.getPage();
//        return ResponseEntity.ok(this.exterUnitCatalogService.queryByPage(data.getExterUnitCatalog(), PageRequest.of(page.getPage(),page.getSize())));
//    }
    @CrossOrigin
    @GetMapping("findAll")
    public ResponseEntity<List<ExterUnitCatalogDTO>> query(){
        return ResponseEntity.ok(this.exterUnitCatalogService.query());
    }
    @CrossOrigin
    @GetMapping("findAll2")
    public ResponseEntity<List<ExterUnitCatalogDTO2>> query2(){
        return ResponseEntity.ok(this.exterUnitCatalogService.query2());
    }
    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @CrossOrigin
    @GetMapping("{id}")
    public ResponseEntity<ExterUnitCatalog> queryById(@PathVariable("id") Integer id) {
        return ResponseEntity.ok(this.exterUnitCatalogService.queryById(id));
    }

    /**
     * 新增数据
     *
     * @param exterUnitCatalog 实体
     * @return 新增结果
     */
    @CrossOrigin
    @PostMapping("add")
    public ResponseEntity<ExterUnitCatalog> add(@RequestBody ExterUnitCatalog exterUnitCatalog) {
        return ResponseEntity.ok(this.exterUnitCatalogService.insert(exterUnitCatalog));
    }

    /**
     * 编辑数据
     *
     * @param exterUnitCatalog 实体
     * @return 编辑结果
     */
    @CrossOrigin
    @PutMapping("put")
    public ResponseEntity<ExterUnitCatalog> edit(@RequestBody ExterUnitCatalog exterUnitCatalog) {
        return ResponseEntity.ok(this.exterUnitCatalogService.update(exterUnitCatalog));
    }

    /**
     * 删除数据
     *
     * param id 主键
     * @return 删除是否成功
     */
    @CrossOrigin
    @DeleteMapping("del")
    public ResponseEntity<Boolean> deleteById(@RequestBody ExterUnitCatalog data) {
        return ResponseEntity.ok(this.exterUnitCatalogService.deleteById(data.getId()));
    }

}

